﻿using Prism.Commands;
using Prism.Mvvm;
using Serial.Helper;
using System;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Windows;
using static Serial.Helper.SerialPortHelper;

namespace Serial.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "串口助手";
        public string Title { get { return _title; } set { SetProperty(ref _title, value); } }
        //是否选中十六进制
        private string _checked1 = "false";
        public string Checked1 { get { return _checked1; } set { SetProperty(ref _checked1, value); } }
        private string _checked2 = "false";
        public string Checked2 { get { return _checked2; } set { SetProperty(ref _checked2, value); } }
        //端口列表
        private ObservableCollection<string> _portList;
        public ObservableCollection<string> PortList { get { return _portList; } set { SetProperty(ref _portList, value); } }
        //传教串口操作助手对象
        public SerialPortHelper _portHelper= new SerialPortHelper();
        //当前选中的波特率
        private int _bote = 5;
        public int Bote { get { return _bote; } set { SetProperty(ref _bote, value); } }
        //当前选中的校验位
        private int _jiaoyan = 0;
        public int Jiaoyan { get { return _jiaoyan; } set { SetProperty(ref _jiaoyan, value); } }
        //当前选中的数据位
        private int _datawei = 2;
        public int Datawei { get { return _datawei; } set { SetProperty(ref _datawei, value); } }
        //当前选中的停止位
        private int _stopwei = 0;
        public int Stopwei { get { return _stopwei; } set { SetProperty(ref _stopwei, value); } }
        //当前选中的端口
        private int _indexOfPort = 0;
        public int IndexOfPort { get { return _indexOfPort; } set { SetProperty(ref _indexOfPort, value); } }
        private string _content1 = "串口未打开";
        public string Content1 { get { return _content1; } set { SetProperty(ref _content1 , value); } }   
         private string _content2 = "打开端口";
        public string Content2 { get { return _content2; } set { SetProperty(ref _content2 , value); } }  
        //禁止打开按钮
        private string _isEnabled = "true";
        public string IsEnabled { get { return _isEnabled; } set { SetProperty(ref _isEnabled, value); } }
        //颜色状态
        private string _color="red";
        public string Color { get { return _color; } set { SetProperty(ref _color, value); } }  
        //打开端口命令
        public DelegateCommand Open { get => new DelegateCommand(openport); }
        //发送的数据
        private string _textOfSend = "";
        public string TextOfSend { get { return _textOfSend; } set { SetProperty(ref _textOfSend , value); } }
        //接收的数据
        private string _textOfAccept= "";
        public string TextOfAccept { get { return _textOfAccept; } set { SetProperty(ref _textOfAccept , value); } }
        //清除数据命令
        public DelegateCommand ClearCommand { get => new DelegateCommand(clearcontenr); }
        //发送数据命令
           public DelegateCommand SendCommand { get => new DelegateCommand(sendcontent); }

        public void sendcontent()
        {
             if (TextOfSend=="")
            {
                MessageBox.Show("发送内容不能为空！", "提示信息");
            }
            else
            {
                //开始发送
                SendData(TextOfSend);
            }
        }
        private void SendData(string data)
        {
            try
            {
                if (Checked1=="true")//发送十六进制数据
                {
                    _portHelper.SendData(data, SendFormat.Hex);
                  
                }
                else  //发送字符串
                {
                    _portHelper.SendData(data, SendFormat.String);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("发送数据出现错误：" + ex.Message, "错误提示！");
            }
        }

        public void clearcontenr()
        {
            TextOfAccept = string.Empty;    
            TextOfSend = string.Empty;
        }
        //打开端口
        public void openport()
        {
            try {
                if (Content2 == "打开端口")
                {
                    _portHelper.OpenSerialPort(_portHelper.PortNames[_indexOfPort], 1);
                    Content1 = "端口已打开";
                    Content2 = "关闭端口";
                    Color= "green";
                }
                else
                {
                    _portHelper.OpenSerialPort(_portHelper.PortNames[_indexOfPort], 0);
                    Content1 = "端口未打开";
                    Content2 = "打开端口";
                    Color= "red";
                }

            }
            catch(Exception ex) {
                  MessageBox.Show("端口操作异常：" + ex.Message);
            }
        }
        //接收数据
         private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                Receive(_portHelper.ReceiveData());
            }
            catch (Exception ex)
            {
                MessageBox.Show("接收数据出现错误：" + ex.Message);
            }
        }
        /// <summary>
        /// 接收数据的具体实现过程
        /// </summary>
        /// <param name="byteData"></param>
        private void Receive(byte[] byteData)
        {
            string data = string.Empty;
            if (Checked2=="true")//十六机制接收
            {
                data = _portHelper.AlgorithmHelperObject.BytesTo16(byteData, Enum16Hex.Blank);
                //在这里编写具体的数据处理过程。。。可以保存到数据库，或其他文件...
            }
            else
            {
                data = _portHelper.AlgorithmHelperObject.BytesToString(byteData, Enum16Hex.None);
            }
            TextOfAccept = data;
          
        }
        public MainWindowViewModel()
        {
            PortList = new ObservableCollection<string>();
            if (_portHelper.PortNames.Length == 0)
            {
                MessageBox.Show("当前计算机上没有找到可用的端口！", "警告信息");
                IsEnabled = "false";
            }
            else
            {
                foreach (var item in _portHelper.PortNames)
                {
                    PortList.Add(item);
                }

            }
            _portHelper.SerialPortObject.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
        }

      
    }
}
